package com.reformer.commons.utils;

import java.io.IOException;
import java.io.OutputStream;

/**
 * @see org.springframework.util.StreamUtils
 */
public abstract class StreamUtil {

    public static final int BUFFER_SIZE = 4096;

    /**
     * 
     * @param outputStream
     * @param bytes
     * @throws IOException
     */
    public static void writeOutputStream(OutputStream outputStream, byte[] bytes) throws IOException {
        int len = bytes.length;
        int offset = 0;
        while (offset < len) {
            int pos;
            if (offset + BUFFER_SIZE > len) {
                pos = len - offset;
            } else {
                pos = BUFFER_SIZE;
            }
            outputStream.write(bytes, offset, pos);
            offset += pos;
            outputStream.flush();
        }
    }
}
